<!doctype html>
<head>
<script src = "../resources/testharness.js"></script>
<script src = "../resources/testharnessreport.js"></script>
</head>
<body>
<script>
var redirect_url =
  'http://127.0.0.1:8000/xmlhttprequest/resources/redirect-loop.php';
var other_redirect_url =
  'http://localhost:8000/xmlhttprequest/resources/redirect-loop.php';

async_test(function(t) {
    var xhr = new XMLHttpRequest;
    xhr.onload = t.step_func(function() {
        assert_unreached('Should fail before redirected 21 times');
      });
    xhr.onerror = function() { t.done(); };
    xhr.open('POST',
             redirect_url + '?Count=21',
             true);
    xhr.send();
  }, 'Redirect count should be limited (same-origin)');

async_test(function(t) {
    var xhr = new XMLHttpRequest;
    xhr.onload = t.step_func(function() {
        assert_unreached('Should fail before redirected 21 times');
      });
    xhr.onerror = function() { t.done(); };
    xhr.open('POST',
             other_redirect_url + '?Count=21',
             true);
    xhr.send();
  }, 'Redirect count should be limited (cross-origin)');

async_test(function(t) {
    var count = 0;
    var onload = function() {
      ++count;
      if (count >= 2) {
        t.done();
      }
    };
    var onerror = t.step_func(function() {
        assert_unreached('Redirecting 20 times should not fail' +
                         'even if there is another resource loading');
      });

    var xhr1 = new XMLHttpRequest;
    xhr1.onload = onload;
    xhr1.onerror = onerror;
    xhr1.open('POST',
              redirect_url + '?Count=20',
              true);
    xhr1.send();

    var xhr2 = new XMLHttpRequest;
    xhr2.onload = onload;
    xhr2.onerror = onerror;
    xhr2.open('POST',
              redirect_url + '?Count=20',
              true);
    xhr2.send();
  }, 'Two XHRs both redirects 20 time should success (same-origin)');

async_test(function(t) {
    var count = 0;
    var onload = function() {
      ++count;
      if (count >= 2) {
        t.done();
      }
    };
    var onerror = t.step_func(function(e) {
        console.log(e);
        assert_unreached('Redirecting 20 times should not fail' +
                         'even if there is another resource loading');
      });

    var xhr1 = new XMLHttpRequest;
    xhr1.onload = onload;
    xhr1.onerror = onerror;
    xhr1.open('POST',
              other_redirect_url + '?Count=20',
              true);
    xhr1.send();

    var xhr2 = new XMLHttpRequest;
    xhr2.onload = onload;
    xhr2.onerror = onerror;
    xhr2.open('POST',
              other_redirect_url + '?Count=20',
              true);
    xhr2.send();
  }, 'Two XHRs both redirects 20 time should success (cross-origin)');

done();
</script>
</body>
